﻿#region Copyright (c) 2010 Tin Pavlinic and Manfred Lange.
//Copyright (c) 2010 Tin Pavlinic and Manfred Lange.

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion

using System;
using System.IO;

namespace NMutate.Console {
   public class Program {
      public static int Main(string[] args) {
         var commandLineResult = 0;
         var workingFolder = string.Empty;
         var systemUnderTest = string.Empty;
         var testSuite = string.Empty;
         if( args.Length > 0 ) {
            workingFolder = args[0];
         }
         if( args.Length > 1 ) {
            systemUnderTest = args[1];
         }
         if( args.Length > 2 ) {
            testSuite = args[2];
         }
         var workFolderAbsolute = Path.Combine(Environment.CurrentDirectory, workingFolder);
         var systemUnderTestPathName = Path.Combine(workFolderAbsolute, systemUnderTest);
         var testSuitePathName = Path.Combine(workFolderAbsolute, testSuite);
         
         if( File.Exists(systemUnderTestPathName)
            && File.Exists(testSuitePathName)) {
            var score = GetAdequacyScore(systemUnderTestPathName, testSuitePathName);
            System.Console.WriteLine("Adequacy score is: " + score);
         }
         else {
            commandLineResult = 1;
         }
         return commandLineResult;
      }

      private static double GetAdequacyScore(string systemUnderTestPathName, string testSuitePathName) {
         var killedCount = 0.0;
         var survivedCount = 0.0;

         foreach(var mutant in new Mutator(systemUnderTestPathName).Mutate()) {
            var testResult = mutant.Execute(testSuitePathName);
            if (testResult == Result.Survived) {
               survivedCount++;
            }
            else {
               killedCount++;
            }
         }

         // Calculate adequacy score:
         return killedCount / (killedCount + survivedCount);
      }
   }
}
